package cn.leon.service.impl;

import cn.leon.common.pojo.EasyUITreeNode;
import cn.leon.mapper.Item_catMapper;
import cn.leon.pojo.Item_cat;
import cn.leon.pojo.Item_catExample;
import cn.leon.service.ItemCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ItemCatServiceImpl implements ItemCatService {
    @Autowired
    private Item_catMapper item_catMapper;

    @Override
    public List<EasyUITreeNode> getItemCatList(long parentId) {
        //根据父节点id查询子节点列表
        Item_catExample item_catExample = new Item_catExample();
        Item_catExample.Criteria criteria = item_catExample.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        //执行查询
        List<Item_cat> item_catList = item_catMapper.selectByExample(item_catExample);
        //转换成EasyUITreeNode对象
        List<EasyUITreeNode> list = new ArrayList<>();
        for (Item_cat item_cat : item_catList) {
            EasyUITreeNode node = new EasyUITreeNode();
            node.setId(item_cat.getId());
            node.setText(item_cat.getName());
            node.setState(item_cat.getIsParent() ? "closed" : "open");
            list.add(node);
        }
        return list;
    }
}

